package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.wan37.client.handler.ClientHandler;
import com.wan37.client.service.DelayThreadPool;
import com.wan37.client.service.PVEFightingService;
import com.wan37.client.service.SkillService;
import com.wan37.protobuf.constant.AllRoleLabel;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.Game;
import com.wan37.protobuf.message.client2server.game.GameEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author : luoyong
 * @date : 2020-07-01 16:18
 **/
@Component(PtoNum.SGAMEATTACKRESPONSE + "")
public class SGameAttackResponse implements ReceiveMsg {
    private final static Logger logger = LoggerFactory.getLogger(SGameAttackResponse.class);
    private ClientHandler clientHandler = ClientHandler.getInstance();

    @Override
    public void execute(Message msg) {
        Game.SGameAttackResponse sGameAttackResponse = (Game.SGameAttackResponse) msg;
        int code = sGameAttackResponse.getCode();
        String content = sGameAttackResponse.getContent();
        String allRoleId = sGameAttackResponse.getSenderId();
        String skillId = sGameAttackResponse.getSkillId();
        switch (code) {
            case Common.GAME_ATTACK_ERROR:
                logger.info("技能: " + skillId + " 释放失败，原因是" + content);
                //将技能重新放回可用技能优先级队列中
                DelayThreadPool.initAdd(allRoleId, SkillService.skillMap.get(skillId));
                break;
            case Common.GAME_ATTACK_SUCCESS:
                clientHandler.updateAOIAllRole(sGameAttackResponse.getAllRoleList());
                //将技能级丢入延迟队列，一定时间后进入可释放技能优先队列
                DelayThreadPool.add(allRoleId, SkillService.skillMap.get(skillId));
                //这里传了被攻击的对象id过来
                String allRoleId2 = sGameAttackResponse.getContent();
                while (true) {
                    GameEntity.Skill skill = SkillService.getPbqSkill(allRoleId);
                    if (skill != null) {
                        clientHandler.sendAttackMessage(allRoleId, allRoleId2, skill.getId());
                        logger.info(allRoleId + " 使用 " +
                                skill.getName() + " 技能攻击了 " + allRoleId2);
                        break;
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case Common.GAME_ATTACK_SUCCESS_FIT_IT:
                clientHandler.updateAOIAllRole(sGameAttackResponse.getAllRoleList());
                sGameAttackResponse.getAllRoleList().forEach(allRole -> {
                    if (allRole.getStatus() == 1) {
                        if (allRole.getLabel() == AllRoleLabel.MONSTER) {
                            logger.info("boss已被击败！！");
                            PVEFightingService.sceneNameToExecutor.get(clientHandler.current_scene).shutdownNow();
                            //这里传了进入boss场景之前的场景名字过来
                            String oldSceneName = sGameAttackResponse.getContent();
                            //战斗结束了，所以需要返回之前的场景
                            clientHandler.sendMoveMessage(oldSceneName);
                        }
                        if (allRole.getLabel() == AllRoleLabel.ROLE) {
                            logger.info("boss已被击败！！");
                            PVEFightingService.sceneNameToExecutor.get(clientHandler.current_scene).shutdownNow();
                            //这里传了进入boss场景之前的场景名字过来
                            String oldSceneName = sGameAttackResponse.getContent();
                            //战斗结束了，所以需要返回之前的场景
                            clientHandler.sendMoveMessage(oldSceneName);
                        }
                    }
                });
                break;
            case Common.GAME_FIGHTING_TIMEOUT:
                logger.info("挑战失败！！");
                PVEFightingService.sceneNameToExecutor.get(clientHandler.current_scene).shutdownNow();
                //这里传了进入boss场景之前的场景名字过来
                String oldSceneName = sGameAttackResponse.getContent();
                //战斗结束了，所以需要返回之前的场景
                clientHandler.sendMoveMessage(oldSceneName);
                break;
            default:
                logger.info("攻击错误的返回！！！");
                break;
        }
    }
}
